﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using System.Transactions;

namespace ZF.WS.fz
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class lfHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            var req = context.Request;
            if (req.Form["params"] != null)
            {
                buygp temp=null;
                try
                {
                     temp = Newtonsoft.Json.JsonConvert.DeserializeObject<buygp>(req.Form["params"]);
                }
                catch
                {
                    context.Response.Write(string.Format("{{'result':{0},'data':'{1}'}}", "false", "参数错误"));
                    context.Response.End();
               
                    return;
                }
                string rbmsg = null;
                var success = false;
                if (temp.type == 0)
                    success = BLL.SiteInfo.Instance.BuyGongPin(temp.oid, temp.bid, temp.x, temp.y, temp.msg, out rbmsg);
                else
                    success = BLL.SiteInfo.Instance.Wishing(temp.bid, temp.msg, out rbmsg);

                context.Response.Write(string.Format("{{'result':{0},'data':'{1}'}}", success.ToString().ToLower(), rbmsg));
                context.Response.End();
            }
            else
            {
                context.Response.Write(string.Format("{{'result':{0},'data':'{1}'}}", "false", "参数错误"));
                context.Response.End();
               
                return;
            }
        }
        
        public string ProcessRequest()
        {
            var member=BLL.SiteInfo.Instance.GetCurrentMember();
            if(member==null)
            {
                return "请登录后操作";
            }else{
                using (var ts = new TransactionScope())
                {
                    using (var dc = BLL.SiteInfo.CreateDBDataContext())
                    {
                        var oid = 0;//供品id
                        var buddhaId = 0;//佛id
                        var msg = "";
                        int x = 0, y = 0;
                        var offering = dc.G_Offering.FirstOrDefault(p => p.OfferingId == oid);
                        var mModel = dc.US_Member.FirstOrDefault(p => p.MemberId == member.MemberId);
                        if (offering == null)
                            return "未找到该供品";
                        else
                        {
                            if (mModel.Integral >= offering.Memoy)
                            {
                                mModel.Integral = mModel.Integral - offering.Memoy;
                                dc.G_Bowing.InsertOnSubmit(new ZF.Model.G_Bowing()
                                {
                                    AddTime = DateTime.Now,
                                    BuddhaId = buddhaId,
                                    MemberId = member.MemberId,
                                    Msg = msg,
                                    X = x,
                                    Y = y,
                                });
                             
                                dc.SubmitChanges();
                                ts.Complete();
                                return "true";
                            }
                            else
                                return "您的积分不足购买该供品";
                        }
                    }
                }
            }
       
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    class buygp
    {
        public int oid;
        public int bid;
        public int x;
        public int y;
        public string msg;
        public int type;//0为拜佛1为许愿
    }
}
